package com.neo.category.web;

import com.neo.category.mapper.CategoryMapper;
import com.neo.category.service.CategoryService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import com.neo.common.domain.Category;
import com.neo.common.feign.GoodsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: chenb
 * @CreateTime: 2024/3/20 13:42
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/category")
@RefreshScope
public class CategoryController {
  //在CategoryServiceImpl定义了@Service实现类所以可以得到默认首字母小写的对象
  @Autowired
  private CategoryService categoryService;

  @Autowired
  private CategoryMapper categoryMapper;

  @Autowired
  private GoodsClient goodsClient;


//  @Value("${pattern.dateformat}")
//  private String dateformat;
//
//  @Value("${lagou.message}")
//  private String message;
//
//  @Value("${database.type}")
//  private String type;
//
//  @Value("${database.type1}")
//  private String type1;

//  @Autowired
//  private DatabaseProperties databaseProperties;

//  @GetMapping("now")
//  public String now(){
//    System.out.println("lagou.message:"+message);
//    System.out.println("lagou.type:"+type);
//    System.out.println("lagou.type1:"+type1);
//    return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
//  }

//  @GetMapping("nowBean")
//  public String nowBean(){
//    return LocalDateTime.now().format(DateTimeFormatter.ofPattern(databaseProperties.getType()));
//  }


  /**
   * 功能描述: Category 保存接口
   */
  @GetMapping("add")
  public Object add(String name,String ext1,String ext2,String ext3){
    Category category = new Category();
    category.setCategoryName(name);
    category.setExt1(ext1);
    category.setExt2(ext2);
    category.setExt3(ext3);
    category.setCreateTime(new Date());
    category.setIsDel("0");
    int id = categoryService.add(category);
    return id;
  }

  /**
   * 功能描述：查找全部
   * 这里和下面是直接调用跳过Servise层，直接到DAO层
   */
  @GetMapping("findAll")
  public Object findAll(){
    System.out.println("CategoryController=>findAll()==>");
    return categoryMapper.getAll();
  }

  /**
   * 查找单个
   */
  @GetMapping("find_by_id")
  public Object findById(long id){
    return categoryMapper.findById(id);
  }

  /**
   * 删除单个
   */
  @GetMapping("del_by_id")
  public Object delById(long id){
    Category category = new Category();
    category.setIsDel("1");
    category.setId((int) id);
//    categoryMapper.delete(id);
    categoryMapper.update(category);
    return "";
  }

  /**
   *更新
   */
  @GetMapping("update")
  public Object update(String name,int id){
    Category category = new Category();
    category.setCategoryName(name);
    category.setId(id);
    categoryMapper.update(category);
    return "";
  }

  @GetMapping("findAllForGoods")
  public Object findAllForGoods(){
    System.out.println("进入findAllForGoods方法");
    return goodsClient.findAll();
  }

}
